package com.edu.cdp.controller;

import com.edu.cdp.DTO.PageDTO;
import com.edu.cdp.DTO.UpdateUserDTO;
import com.edu.cdp.DTO.UserDTO;
import com.edu.cdp.DTO.UserLoginDTO;
import com.edu.cdp.utils.CodeUtils;
import com.edu.cdp.VO.BalanceVO;
import com.edu.cdp.common.PageResult;
import com.edu.cdp.common.Result;
import com.edu.cdp.domain.User;
import com.edu.cdp.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@Slf4j
@Tag(name = "用户相关接口",
        description = "此接口用户用户数据相关操作，")
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    CodeUtils codeUtils;
    @PostMapping("/login")
    @Operation(summary = "用户登录接口")
    public Result<User> login(@RequestBody UserLoginDTO userDTO){
        System.out.println("登录："+ userDTO.getUsername() +" /n"+userDTO.getUsername());
        User user = userService.login(userDTO.getUsername(),
                DigestUtils.md5DigestAsHex(userDTO.getPassword().getBytes()));

        if (user != null) {
            return Result.success(user);
        } else {
            return Result.error("用户名或密码错误");
        }
    }

    @PostMapping("/register")
    @Operation(summary = "用户注册接口",
            description="此接口用于客户注册账号，每个qq邮箱只能注册两个账户")
    public Result<User> register(@RequestBody UserDTO userDTO) {
        int result = codeUtils.contrastCode(userDTO.getEmail(), userDTO.getCode());
        if (result == 1){
            User newUser = userService.register(userDTO);
            if (newUser != null) {
                return Result.success(newUser);
            } else {
                return Result.error("该邮箱注册用户超过限度");
            }
        } else if (result == 0) {
            return Result.error("验证码已失效");
        } else {
            return Result.error("验证码错误");
        }
    }

    @GetMapping("/list")
    @Operation(summary = "获取用户列表")
    public Result<List<User>> list(){
        List<User> list = userService.list();
        return Result.success(list);
    }

    @GetMapping("selectById")
    @Operation(summary = "根据id获取用户")
    public Result<User> selectById(@Parameter(name = "id", description = "用户id") Integer id){
        User user = userService.getById(id);

        return Result.success(user);
    }

    @GetMapping("/listByName")
    @Operation(summary = "根据用户名模糊查询")
    public Result<List<User>> listByName(
                @Parameter(name = "username", description = "用户姓名")
                                             String username){
        List<User> users = userService.getUserByName(username);

        return Result.success(users);
    }

    @GetMapping("/listBySex")
    @Operation(summary = "根据性别查询用户")
    public Result<List<User>> listBySex(@Parameter(name = "sex",
            description = "用户性别")  String sex){
        List<User> users = userService.listBySex(sex);

        return Result.success(users);
    }

    @GetMapping("/page")
    @Operation(summary = "用户分页查询")
    public Result<PageResult> page(@ParameterObject PageDTO pageDTO){
        PageResult pageResult = userService.pageQuery(pageDTO);

        return Result.success(pageResult);
    }

    @PutMapping("/update")
    @Operation(summary = "修改用户", description = "此接口用于用户修改信息，" +
            "邮箱做为识别不可修改")
    public Result<User> update(@ParameterObject UpdateUserDTO user){
        User newuser = userService.newUpdate(user);

        if (newuser != null){
            return Result.success(newuser);
        }
        return Result.error("修改失败");
    }

    @PostMapping(value = "/avatar")
    @Operation(summary = "更新头像",
            description = "此接口用于用户更新头像")
    public Result<User> updateAvatar(@Parameter(name = "id", description = "用户id")
                                         Integer id,
                                     @Parameter(name = "avatar", description = "用户头像")
                                      MultipartFile avatar) {
        User user = userService.updateAvatar(id, avatar);
        if (user != null) {
            return Result.success(user);
        } else {
            return Result.error("查无此用户");
        }
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户", description = "此接口用于删除用户")
    public Result<String> delete(@Parameter(name = "id", description = "用户id")
                                     Integer id){
        boolean result = userService.removeById(id);

        if (result) {
            return Result.success();
        } else {
            return Result.error("删除失败");
        }
    }

    @GetMapping("/balance")
    @Operation(summary = "用户查询余额")
    public Result<BalanceVO> balance(@Parameter(name = "id", description = "用户id")
                                         Integer id){
        BalanceVO balanceVO = userService.getBalance(id);

        return Result.success(balanceVO);
    }

    @PutMapping("/recharge")
    @Operation(summary = "用户充值")
    public Result<BalanceVO> recharge(@Parameter(name = "id", description = "用户id")
                                          Integer id,
                                      @Parameter(name = "money", description = "用户充值的金额")
                                      Double money){
        BalanceVO balanceVO = userService.recharge(id, money);

        if (balanceVO == null) {
            return Result.error("充值失败");
        } else {
            return Result.success(balanceVO);
        }
    }

    @PutMapping("/consume")
    @Operation(summary = "用户消费")
    public Result<User> consume(
            @Parameter(name = "id", description = "用户id")
            Integer id,
            @Parameter(name = "money", description = "用户消费的金额")
            Double money){
           User user = userService.consume(id, money);
           if (user ==null) {
               return Result.error("消费失败");
           } else {
               return Result.success(user);
           }
    }
}
